#include "sendfilebox.h"
#include "ui_sendfilebox.h"
#include <QDebug>
SendFileBox::SendFileBox(QWidget *parent) :
    FileBox(parent),
    ui(new Ui::SendFileBox)
{
    ui->setupUi(this);
    ui->btn_sendfile->setIconSize(QSize(120,120));
}

SendFileBox::~SendFileBox()
{
    delete ui;
}

void SendFileBox::setAvatar(const QPixmap avatar)
{
    ui->label_sendfileavatar->setPixmap(avatar);
}
void SendFileBox::setText(const QString text){
    filepath=text;
    QString tmp=text;
    int index=-1;
    for(int i=0;i<5;i++)
    {
        index=tmp.indexOf('/',index+1);
    }
    filename=text.mid(index+1);
    //if(!isnew)
    //   ui->label->setText(insertNewLinesForLabel(ui->label->font(),filename+"\n等待发送",ui->label->width()));
    //else
        ui->label->setText(insertNewLinesForLabel(ui->label->font(),filename,ui->label->width()));
}
void SendFileBox::setImg(QPixmap img){
    ui->btn_sendfile->setIcon(img);
}

QSize SendFileBox::getSize()
{
    return this->sizeHint();
}

QString SendFileBox::getFileName()
{
    return filename;
}

void SendFileBox::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton&&static_cast<QPushButton*>(childAt(event->pos()))==ui->btn_sendfile) {
        QPoint globalPos = this->mapToGlobal(event->pos());
        QMenu menu;
        // 添加菜单项...
        //QAction *open = menu.addAction("打开");
        QAction *favorite=menu.addAction("收藏");
        connect(favorite, &QAction::triggered, [=](){
            emit s_addFavorite(filepath,filename,QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
        });
        menu.exec(globalPos); // 在鼠标位置显示菜单
    }
    QWidget::mousePressEvent(event);
}





void SendFileBox::on_btn_sendfile_clicked()
{
    QFileInfo f(filepath);
    QDesktopServices::openUrl(f.absoluteFilePath());
}

//设置进度文本
void SendFileBox::setProcess(float value)
{
    //qDebug()<<value;
    ui->label->clear();
    ui->label->setText(filename+"\n"+QString::number((int)value)+"%");
}

void SendFileBox::resetText()
{
    ui->label->clear();
    ui->label->setText(filename);
}


